package com.shadow.bus.adapter;

import android.content.Context;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v7.widget.AppCompatImageView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shadow.bus.config.ItemFiled;
import com.shadow.bus.config.ItemType;
import com.shadow.bus.entity.PartEntity;
import com.shadow.bus.entity.PersonEntity;
import com.shadow.bus.entity.TroubleEntity;
import com.shadow.bus.entity.TroubleOperationEntity;
import com.tepth.shadow.ui.recycler.MultipleItemEntity;
import com.tepth.shadow.ui.recycler.MultipleRecyclerAdapter;
import com.tepth.shadow.ui.recycler.MultipleViewHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

import busfix.tepth.com.shadow_bus.R;

/**
 * @author zhangyu
 * @date 2018/7/5
 */

public class MultipleTypeAdapter extends MultipleRecyclerAdapter {
    private VectorDrawableCompat iconAdd;
    private VectorDrawableCompat iconMinus;
    private VectorDrawableCompat iconDelete;
    private LayoutInflater mInflater;
    private List<TroubleOperationEntity> mSelectList;

    public MultipleTypeAdapter(Context context,
                               List<MultipleItemEntity> data,
                               List<TroubleOperationEntity> selectList) {
        super(data);
        mInflater = LayoutInflater.from(context);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_HEAD, R.layout.item_trouble_head);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_CONTENT, R.layout.item_trouble_content);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_WORK, R.layout.item_trouble_operator_work);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_PUNCH, R.layout.item_trouble_operator_punch);
        addItemType(ItemType.TYPE_ITEM_TROUBLE_OPERATOR_REWORK, R.layout.item_trouble_operator_rework);
        iconAdd = VectorDrawableCompat.create(context.getResources(),
                R.drawable.vector_drawable_icon_add, context.getTheme());
        iconMinus = VectorDrawableCompat.create(context.getResources(),
                R.drawable.vector_drawable_ic_minus, context.getTheme());
        iconDelete = VectorDrawableCompat.create(context.getResources(),
                R.drawable.vector_drawable_ic_delete, context.getTheme());
        mSelectList = selectList;
    }

    @Override
    protected void convert(final MultipleViewHolder helper, final MultipleItemEntity item) {
        switch (item.getItemType()) {
            case ItemType.TYPE_ITEM_TROUBLE_HEAD:
                helper.setText(R.id.tv_header, (CharSequence) item.getField(ItemFiled.FILED_NAME));
                break;
            case ItemType.TYPE_ITEM_TROUBLE_CONTENT:
                TroubleEntity troubleEntity = item.getField(ItemFiled.ITEM_OBJ);
                if (troubleEntity != null) {
                    if (troubleEntity.getName() != null) {
                        helper.setText(R.id.tv_name, troubleEntity.getName().replace("自定义录入@@", "附加作业项"));
                    }
                }
                helper.addOnClickListener(R.id.tv_add);
                break;
            case ItemType.TYPE_ITEM_TROUBLE_OPERATOR_WORK:
                final TroubleOperationEntity operationWork = item.getField(ItemFiled.ITEM_OBJ);
                if (operationWork != null) {
                    helper.setChecked(R.id.checkbox, operationWork.isChecked());
                    helper.getView(R.id.checkbox).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            operationWork.setChecked(!operationWork.isChecked());
                            if (mSelectList != null) {
                                if (operationWork.isChecked()) {
                                    mSelectList.add(operationWork);
                                } else {
                                    mSelectList.remove(operationWork);
                                }
                            }
                        }
                    });
                    AppCompatImageView ivAdd = helper.getView(R.id.iv_add);
                    ivAdd.setImageDrawable(iconAdd);
                    ivAdd.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            operationWork.setUseCount(operationWork.getUseCount() + 1);
                            helper.setText(R.id.tv_count, operationWork.getUseCount() + "");
                        }
                    });
                    AppCompatImageView ivMinus = helper.getView(R.id.iv_minus);
                    ivMinus.setImageDrawable(iconMinus);
                    ivMinus.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (operationWork.getUseCount() <= 1) {
                                return;
                            }
                            operationWork.setUseCount(operationWork.getUseCount() - 1);
                            helper.setText(R.id.tv_count, operationWork.getUseCount() + "");
                        }
                    });
                    AppCompatImageView ivDelete = helper.getView(R.id.iv_delete);
                    ivDelete.setImageDrawable(iconDelete);
                    String des = item.getField(ItemFiled.ITEM_TROUBLE_NAME);
                    if (des != null) {
                        helper.setText(R.id.tv_trouble_des, des.replaceAll("自定义录入@@", "附加作业项"));
                    }
                    if (operationWork.getOperation() != null) {
                        helper.setText(R.id.tv_operator, operationWork.getOperation().getName());
                    }
                    PartEntity part = operationWork.getAccessory() == null ? operationWork.getComponent() : operationWork.getAccessory();
                    if (part != null) {
                        helper.setText(R.id.tv_part, part.getName());
                        helper.setText(R.id.tv_count, operationWork.getUseCount() + "");
                        helper.setText(R.id.tv_unit, part.getUnit());
                    }
                    helper.addOnClickListener(R.id.iv_delete);
                    helper.addOnClickListener(R.id.tv_add_trouble);
                    TagFlowLayout tagFlowLayout = helper.getView(R.id.tag_flow_layout);
                    final TagAdapter<TroubleEntity> tagAdapter =
                            new TagAdapter<TroubleEntity>(operationWork.getChildren()) {
                                @Override
                                public View getView(FlowLayout parent, int position,
                                                    TroubleEntity troubleBean) {
                                    LinearLayout layout = (LinearLayout) mInflater.inflate(R.layout.view_add_trouble_tv,
                                            parent, false);
                                    TextView tv = layout.findViewById(R.id.tv);
                                    tv.setText(troubleBean.getName());
                                    return layout;
                                }
                            };
                    tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                        @Override
                        public boolean onTagClick(View view, int position, FlowLayout parent) {
                            TroubleEntity remarkTrouble = operationWork.getChildren().get(position);
                            List<TroubleOperationEntity> troubleBeans = remarkTrouble.getShareOperation();
                            if (troubleBeans != null) {
                                troubleBeans.remove(operationWork);
//                            if (mClickListener != null) {
//                                mClickListener.onClick();
//                            }
                            }
                            operationWork.getChildren().remove(position);
                            tagAdapter.notifyDataChanged();
                            return false;
                        }
                    });
                    tagFlowLayout.setMaxSelectCount(1);
                    tagFlowLayout.setAdapter(tagAdapter);
                }
                break;
            case ItemType.TYPE_ITEM_TROUBLE_OPERATOR_PUNCH:
                TroubleOperationEntity operationPunch = item.getField(ItemFiled.ITEM_OBJ);
                if (operationPunch != null) {
                    String des = item.getField(ItemFiled.ITEM_TROUBLE_NAME);
                    if (des != null) {
                        helper.setText(R.id.tv_trouble_des, des.replaceAll("自定义录入@@", "附加作业项"));
                    }
                    if (operationPunch.getOperation() != null) {
                        helper.setText(R.id.tv_operator, operationPunch.getOperation().getName());
                    }
                    PartEntity partPunch = operationPunch.getAccessory() == null ? operationPunch.getComponent() : operationPunch.getAccessory();
                    if (partPunch != null) {
                        helper.setText(R.id.tv_part, partPunch.getName());
                        helper.setText(R.id.tv_count, operationPunch.getUseCount() + "");
                        helper.setText(R.id.tv_unit, partPunch.getUnit());
                    }
                    PersonEntity personEntity = operationPunch.getWorkPerson();
                    helper.setText(R.id.tv_person, personEntity == null ? "无打卡人" : personEntity.getName());
                    helper.addOnClickListener(R.id.tv_cancel);
                }
                break;
            case ItemType.TYPE_ITEM_TROUBLE_OPERATOR_REWORK:
                TroubleOperationEntity operationRework = item.getField(ItemFiled.ITEM_OBJ);
                if (operationRework != null) {
                    String des = item.getField(ItemFiled.ITEM_TROUBLE_NAME);
                    if (des != null) {
                        helper.setText(R.id.tv_trouble_des, des.replaceAll("自定义录入@@", "附加作业项"));
                    }
                    if (operationRework.getOperation() != null) {
                        helper.setText(R.id.tv_operator, operationRework.getOperation().getName());
                    }
                    PartEntity partRework = operationRework.getAccessory() == null ? operationRework.getComponent() : operationRework.getAccessory();
                    if (partRework != null) {
                        helper.setText(R.id.tv_part, partRework.getName());
                        helper.setText(R.id.tv_count, operationRework.getUseCount() + "");
                        helper.setText(R.id.tv_unit, partRework.getUnit());
                    }
                    PersonEntity personRework = operationRework.getWorkPerson();
                    helper.setText(R.id.tv_person, personRework == null ? "无打卡人" : personRework.getName());
                }
                break;
        }
    }
}
